package com.jcl.recruit.elasticsearch.core.builds;

import com.jcl.core.constants.Constant;
import com.jcl.core.utils.CollectionUtils;
import com.jcl.core.utils.DateUtils;
import com.jcl.core.utils.StringUtils;
import com.jcl.recruit.elasticsearch.core.entity.vo.SearchResumeRecord;
import com.jcl.recruit.elasticsearch.core.property.ElasticConditionProperty;
import com.jcl.recruit.elasticsearch.core.tools.*;

import java.util.Date;
import java.util.List;

/**
 * @ClassName ElasticCondition

 * @Author yyliu
 * @Date 2018/10/26 15:03
 * @Version 1.0
 **/
public class ElasticCondition {

    public ElasticCondition() {
    }

    public Condition conditionResumeJson(List<SearchResumeRecord> entityList,
                                         String channel,
                                         String corpCode,
                                         Integer curPage,
                                         Integer resumeUpdateDays,
                                         Integer comingDays) {
        Condition condition = new Condition();
        if (curPage == null || curPage == 0) {
            curPage = 1;
        }
        QueryBool queryBool = new QueryBool();
        condition.setQuery(queryBool);
        if (CollectionUtils.isNotEmpty(entityList)) {
            if (entityList.size() == 1) {
                Must searchMust = mdBool(entityList.get(0), channel, corpCode, resumeUpdateDays, comingDays);
                queryBool.setMust(searchMust.getMust());
            } else {
                Should should = new Should();
                for (SearchResumeRecord entity : entityList) {
                    ShouldBool shouldBool = new ShouldBool();
                    Must searchMust = mdBool(entity, channel, corpCode, resumeUpdateDays, comingDays);
                    shouldBool.setMust(searchMust.getMust());
                    should.setShould(shouldBool);
                }
                queryBool.setShould(should.getShould());
            }
        } else {
            Must must = new Must();
            // 渠道
            if (StringUtils.hasLength(channel)) {
                Match match = new Match();
                match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CHANNEL, channel);
                must.setMatch(match);
            }
            //  企业
            if (StringUtils.hasLength(corpCode)) {
                Match match = new Match();
                match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CORP_CODE, corpCode);
                must.setMatch(match);
            }
            if(CollectionUtils.isNotEmpty(must.getMust())){
                queryBool.setMust(must.getMust());
            }
        }
        // 排序
        condition.setSort(ElasticConditionProperty.PROPERTY_RESUME_UPDATE_DATE,
                ElasticConditionProperty.PROPERTY_SORT_DESC);
        condition.setSort(ElasticConditionProperty.PROPERTY_SCORE,
                ElasticConditionProperty.PROPERTY_SORT_DESC);
        condition.setFrom(curPage - Constant.NUMBER_1);
        return condition;
    }

    private Must mdBool(SearchResumeRecord entity,
                        String channel,
                        String corpCode,
                        Integer resumeUpdateDays,
                        Integer comingDays) {
        Must must = new Must();
        // 时间设定
        if (null == resumeUpdateDays) {
            resumeUpdateDays = ElasticConditionProperty.DEGAULT_RESUME_DAYS;
        }
        Range rangeUpdateResumeTime = new Range();
        // 如果用yyyy-MM-dd HH:mm:ss格式es会报错
        rangeUpdateResumeTime.setRange(ElasticConditionProperty.PROPERTY_RESUME_UPDATE_DATE,
                DateUtils.getDateTime(new Date(), -resumeUpdateDays).toString(), null);
        must.setRange(rangeUpdateResumeTime);
        if (null != comingDays) {
            Range rangeComingTime = new Range();
            // 如果用yyyy-MM-dd HH:mm:ss格式es会报错
            rangeUpdateResumeTime.setRange(ElasticConditionProperty.PROPERTY_RESUME_CREATE_DATE,
                    DateUtils.getDateTime(new Date(), -comingDays).toString(), null);
            must.setRange(rangeComingTime);
        }
        // 工作年限 1=应届生； 2=[1,3]；3=[3,5]；4= 5及以上
        if (null != entity.getWorkYear()) {
            Range range = new Range();
            if (1 == entity.getWorkYear()) { // workyear <=1
                range.setRange(ElasticConditionProperty.PROPERTY_RESUME_WORK_YEAR, null, "1");
            }
            if (2 == entity.getWorkYear()) { // workyear [1,3]
                range.setRange(ElasticConditionProperty.PROPERTY_RESUME_WORK_YEAR, "1", "3");
            }
            if (3 == entity.getWorkYear()) { // workyear = [3,5]
                range.setRange(ElasticConditionProperty.PROPERTY_RESUME_WORK_YEAR, "3", "5");
            }
            if (5 == entity.getWorkYear()) { // workyear >=5
                range.setRange(ElasticConditionProperty.PROPERTY_RESUME_WORK_YEAR, "5", null);
            }
            must.setRange(range);
        }
        //  渠道
        if (StringUtils.hasLength(channel)) {
            Match match = new Match();
            match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CHANNEL, channel);
            must.setMatch(match);
        }
        //  企业
        if (StringUtils.hasLength(corpCode)) {
            Match match = new Match();
            match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CORP_CODE, corpCode);
            must.setMatch(match);
        }
        // 学历
        if (StringUtils.hasLength(entity.getEducation())) {
            Match match = new Match();
            match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_EDUCATION, entity.getEducation());
            must.setMatch(match);
        }

        // 城市
        if (StringUtils.hasLength(entity.getCity())) {
            MustBool mustBool = new MustBool();
            Should boolShould = new Should();

            Match match = new Match();
            match.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CITY2, entity.getCity());
            Match match2 = new Match();
            match2.setMatch(ElasticConditionProperty.PROPERTY_RESUME_CITY, entity.getCity());
            boolShould.setShould(match);
            boolShould.setShould(match2);
            /*Wildcard wildcard1 = new Wildcard();
            wildcard1.setWildcard(ElasticConditionProperty.PROPERTY_RESUME_CITY, entity.getCity());
            Wildcard wildcard2 = new Wildcard();
            wildcard2.setWildcard(ElasticConditionProperty.PROPERTY_RESUME_CITY2, entity.getCity());
            boolShould.setShould(wildcard1);
            boolShould.setShould(wildcard2);*/

            mustBool.setShould(boolShould.getShould());
            must.setBool(mustBool);

        }
        // 关键字
        if (StringUtils.hasLength(entity.getKeyword())) {
            MustBool mustBool = new MustBool();
            Should boolShould = new Should();

            if(null == mustBool){
                mustBool = new MustBool();
            }
            if(null == boolShould){
                boolShould = new Should();
            }
            String[] keyword = entity.getKeyword().split(" ");

            Wildcard wildcard_keyword = new Wildcard();
            wildcard_keyword.setWildcard(ElasticConditionProperty.PROPERTY_RESUME_KW_KEYWORD, keyword);
            Wildcard wildcard_position = new Wildcard();
            wildcard_position.setWildcard(ElasticConditionProperty.PROPERTY_RESUME_KW_POSITION, keyword);
            Wildcard wildcard_position2 = new Wildcard();
            wildcard_position2.setWildcard(ElasticConditionProperty.PROPERTY_RESUME_POSITION, keyword);
            boolShould.setShould(wildcard_keyword);
            boolShould.setShould(wildcard_position);
            boolShould.setShould(wildcard_position2);

            mustBool.setShould(boolShould.getShould());
            must.setBool(mustBool);

        }

        return must;
    }


}
